//
// Created by shirley on 2024/12/23.
//

#ifndef DENSEINVERSION_H
#define DENSEINVERSION_H
#include <memory>

#include "DenseMatrix.h"

class DenseSolver
{
    private:
        const DenseMatrix& matrix;
        DenseMatrix invMatrix;
        string type;
        string function;
        int thread;
        //获取矩阵matrix的数据引用
        vector<vector<double>>& data_(shared_ptr<DenseMatrix>& matrix);
        //获取矩阵matrix分块后的子矩阵
        vector<DenseMatrix>& blocks_(shared_ptr<DenseMatrix>& matrix);
        //矩阵LU分解
        bool lU_Decomposition(shared_ptr<DenseMatrix>& L, shared_ptr<DenseMatrix>& U, const DenseMatrix& matrix = DenseMatrix(0,0));
        //矩阵分块LU分解
        bool blockLU_Decomposition(shared_ptr<DenseMatrix>& L, shared_ptr<DenseMatrix>& U, const DenseMatrix& matrix,size_t blockSize = 100);
        //向前替换求解 Ly = b
        DenseMatrix forward_substitution(const DenseMatrix& L, const DenseMatrix& b);
        //向后替换求解 Ux = y
        DenseMatrix backward_substitution(const DenseMatrix& U, const DenseMatrix& y);
        //LU分解求逆
        bool LU_Inverse();
        //LU分解求解 Ax = b
        DenseMatrix LU_Solve(DenseMatrix& B);
        //LU分解求解不同值向量的线性方程组组
        vector<DenseMatrix> LU_Solve(vector<DenseMatrix>& B);
        //LU分解求解线性方程组
        DenseMatrix solveLinearSystem(shared_ptr<DenseMatrix>& L, shared_ptr<DenseMatrix>& U, const DenseMatrix& b);
    public:
        DenseSolver(const DenseMatrix& matrix, int thread = 1, string function = "TPS", string type = "LU");
        ~DenseSolver() = default;
        //矩阵求逆
        bool Inverse();
        //矩阵求解 Ax = b
        DenseMatrix solve(DenseMatrix& B);
        //矩阵求解多个线性方程组 A * x[i] = B[i]
        vector<DenseMatrix> solve(vector<DenseMatrix>& B);
        //获取矩阵的逆矩阵
        const DenseMatrix& invMatrix_() const;
};



#endif //DENSEINVERSION_H
